package com.epoint.BookManager.utils;


import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Properties;

import javax.sql.DataSource;


import com.alibaba.druid.pool.DruidDataSourceFactory;



public class JDBCUtil
{
    private static DataSource dSource;


    public static DataSource getDataSource() {
        if (null == dSource) {
            try {
                Properties properties = new Properties();
                properties.load(JDBCUtil.class.getClassLoader().getResourceAsStream("mysql.properties"));
                dSource = DruidDataSourceFactory.createDataSource(properties);
            }
            catch (Exception e) {
                e.printStackTrace();
            }
        }

        return dSource;
    }

    public static Connection getConnection() {

        Connection connection = null;

        try {
            dSource = JDBCUtil.getDataSource();
            connection = dSource.getConnection();
        }
        catch (Exception e) {
            e.printStackTrace();
        }
        return connection;
    }

    public static void closeConnection(Connection conn, Statement stmt, ResultSet rs) {
        try {
            if (null != conn) {
                conn.close();
            }
            if (null != stmt) {
                stmt.close();
            }
            if (null != rs) {
                rs.close();
            }
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }
    


}
